package org.opu.pc.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Yuriy Dunko
 */
public class StringUtil {

    /**
     * For example you have string with some keys: <br>
     * <pre>
     * {@code "Hello Mr ${name}, I want to congratulate you with ${celebration}."}
     * </pre>
     * And you want to replace keys in bracket on the real value. It will be
     * look like this:
     * <pre>
     * {@code
     * String template = "Hello Mr ${name}, I want to congratulate you with ${celebration}"
     * Map<String, String> values = new HashMap<String, String>();
     * values.put("name", "Broun");
     * values.put("celebration", "birthday");
     * String result = assignVarValue(template, values);
     * // String result will contain: "Hello Mr Broun, I want to congratulate you with birthday"
     * }
     * </pre>
     *
     * @param string template with the keys
     * @param values map that contain value for each key. If this map will not
     * contain some value you will get exception!
     * @return parsed string
     */
    public static String assignVarValue(String string, Map<String, String> values) {
        return assignVarValue(0, string, values);
    }

    /**
     * @see #assignVarValue(java.lang.String, java.util.Map)
     * @param startPoint position where from start parsing
     */
    public static String assignVarValue(int startPoint, String string,
            Map<String, String> values){

        int start, end;

        if ((start = string.indexOf("${", startPoint)) > 0
                && (end = string.indexOf("}", start)) > 0){
            String v;
            String key = string.substring(start + 2, end );
            string = string.substring(0, start)
                    + (v = values.get(key))
                    + string.substring(end + 1, string.length());

            if (v == null){
                throw new IllegalArgumentException(String.format("values [%s],"
                        + " do not containe [%s]", values.toString(), key));
            }


            return assignVarValue(start + v.length(), string, values);
        }

        return string;
    }

    public static Map<String, String> propertiesToMap(Properties p){
        HashMap<String, String> map = new HashMap<String, String>();
        for (Map.Entry<?, ?> e : p.entrySet()) {
            map.put((String) e.getKey(), (String) e.getValue());
        }
        return map;
    }

    public static Map<String, String> toMap(String... string){
        Map<String, String> map = new HashMap<String, String>();
        for (int i = 0; i < string.length; i+= 2) {
            map.put(string[i], string[i+1]);
        }
        return map;
    }

    public static boolean contains(String string, String regEx){
        Pattern p = Pattern.compile(regEx);
        Matcher matcher = p.matcher(string);
//        boolean b = matcher.find();
        return matcher.find();//Pattern.matches(regEx, string);
    }

}
